package com.mobilesales.android.database;

import java.util.ArrayList;
import java.util.List;

import com.mobilesales.android.model.orders.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class OrderManager extends ManagerBase{	
	public OrderManager(Context c)
	{
		super(c);
	}
	
	public List<Order> GetOrderList()
	{
     	try {
     		
     		String query = "SELECT _id"
     						+ " FROM Orders";
     		Cursor c = db.rawQuery(query, null);
     		
     		List<Order> result = new ArrayList<Order>();
     		if (!c.moveToFirst())
     			return result;
     		
     		List<Integer> tmp = new ArrayList<Integer>();
     		while (!c.isAfterLast()){
     			tmp.add(c.getInt(c.getColumnIndex("_id")));
     			c.moveToNext();
     		}
     		c.close();
     		for (int id: tmp)
     			result.add(GetOrder(id));	
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public Order GetOrder(int orderId)
	{
     	try {
     		
     		String query = "SELECT _id, OrderStatusId, ClientId, Date, CreatorId" 
							+ " FROM Orders" 
							+ " WHERE _id = " + orderId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;

     		Order result = new Order();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.CreatorId = c.getInt(c.getColumnIndex("CreatorId"));
     		result.ClientId = c.getInt(c.getColumnIndex("ClientId"));
     		result.Date = c.getString(c.getColumnIndex("Date"));
     		int orderStatusId = c.getInt(c.getColumnIndex("OrderStatusId"));
     		
     		c.close();
     		result.Status = GetOrderStatus(orderStatusId);
     		result.Items = GetOrderItemList(result.Id);
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public boolean StoreOrder(Order order)
	{
     	try {
     		int result;
			ContentValues cv = new ContentValues();

			cv.put("_id", order.Id);
			cv.put("ClientId",order.ClientId);
			cv.put("Date", order.Date);
			cv.put("OrderStatusId", order.Status.Id);
			cv.put("CreatorId", order.CreatorId);
			
			result = db.update("Orders", cv, "_id ="+order.Id, null);
			if (result == 0)
				result = db.insert("Orders", null, cv) != -1 ? 1 : 0;
			
			if (result <= 0)
				return false;
			for (OrderItem o : order.Items)
				if (!StoreOrderItem(o))
					throw new Exception("Order item failed to store " + o.Id);	
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public boolean RemoveOrder(int orderId)
	{
     	try {
     		int result;
			result = db.delete("Orders", "_id ="+orderId, null);
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public OrderItem GetOrderItem(int orderItemId)
	{
     	try {
     		dbHelper.openDatabase();
     		
     		String query = "SELECT _id, ProductId, OrderId, Quantity" 
							+ " FROM OrderElements" 
							+ " WHERE _id = " + orderItemId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		OrderItem result = new OrderItem();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		int productId = c.getInt(c.getColumnIndex("ProductId"));
     		result.OrderId = c.getInt(c.getColumnIndex("OrderId"));
     		result.Quantity = c.getDouble(c.getColumnIndex("Quantity"));
     		
     		c.close();
     		result.Product = new ProductManager(context).GetProduct(productId);
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public ArrayList<OrderItem> GetOrderItemList(int orderId)
	{
     	try {
     		
     		String query = "SELECT _id" 
							+ " FROM OrderElements" 
							+ " WHERE OrderId = " + orderId;
     		Cursor c = db.rawQuery(query, null);
     		
     		ArrayList<OrderItem> result = new ArrayList<OrderItem>();
     		if (!c.moveToFirst())
     			return result;
     		

     		List<Integer> tmp = new ArrayList<Integer>();
     		while (!c.isAfterLast()){
     			tmp.add(c.getInt(c.getColumnIndex("_id")));
     			c.moveToNext();
     		}
     		c.close();
     		for (int id : tmp)
     			result.add(GetOrderItem(id));    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public boolean StoreOrderItem(OrderItem orderItem)
	{

     	try {
     		int result;
			ContentValues cv = new ContentValues();
			
			if (orderItem.Product != null)
				cv.put("ProductId",orderItem.Product.Id);
			else
				cv.putNull("ProductId");
			cv.put("_id", orderItem.Id);
			cv.put("OrderId", orderItem.OrderId);
			cv.put("Quantity", orderItem.Quantity);
			
			result = db.update("OrderElements", cv, "_id ="+orderItem.Id, null);
			if (result == 0)
				result = db.insert("OrderElements", null, cv) != -1 ? 1 : 0;
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public boolean RemoveOrderItem(int orderItemId)
	{
     	try {
     		int result;
			result = db.delete("OrderElements", "_id ="+orderItemId, null);
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public List<OrderStatus> GetOrderStatusList()
	{
     	try {
     		
     		String query = "SELECT _id"
     						+ " FROM OrderStatus";
     		Cursor c = db.rawQuery(query, null);
     		
     		List<OrderStatus> result = new ArrayList<OrderStatus>();
     		if (!c.moveToFirst())
     			return result;
     		
     		List<Integer> tmp = new ArrayList<Integer>();
     		while (!c.isAfterLast()){
     			tmp.add(c.getInt(c.getColumnIndex("_id")));
     			c.moveToNext();
     		}
     		c.close();
     		for (int id : tmp)
     			result.add(GetOrderStatus(id));	
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	
	public OrderStatus GetOrderStatus(int orderStatusId)
	{
     	try {
     		
     		String query = "SELECT _id, StatusName" 
							+ " FROM OrderStatus" 
							+ " WHERE _id = " + orderStatusId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		OrderStatus result = new OrderStatus();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.Name = c.getString(c.getColumnIndex("StatusName"));
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	
		return null;
	}
	public boolean StoreOrderStatus(OrderStatus orderStatus)
	{
     	try {
     		int result;
			ContentValues cv = new ContentValues();

			cv.put("_id", orderStatus.Id);
			cv.put("StatusName",orderStatus.Name);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			result = db.update("OrderStatus", cv, "_id ="+orderStatus.Id, null);
			if (result == 0)
				result = db.insert("OrderStatus", null, cv) != -1 ? 1 : 0;
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
	
	public boolean RemoveOrderStatus(int orderStatusId)
	{
     	try {
     		int result;
			result = db.delete("OrderStatus", "_id ="+orderStatusId, null);
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     		return false;
     	}
	}
}
